Given a string s
, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Input: s = "bcabc" Output: "abc"
Input: s = "cbacdcbc" Output: "acdb"
1 <= s.length <= 104
s
consists of lowercase English letters.
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
implSolution{pubfnremove_duplicate_letters(s:String) -> String{let s = s.as_bytes();letmut mask = 0;letmut masks = vec![0; s.len()];letmut indices = vec![vec![];26];letmut i = 0;letmut ret = vec![];for j in(0..s.len()).rev(){ masks[j] = mask; mask |= 1 << (s[j] - b'a'); indices[(s[j] - b'a')asusize].push(j);}while mask != 0{for j in(0..=26).filter(|x| mask &(1 << x) != 0){let new_mask = mask ^ (1 << j);let k = match indices[j].binary_search_by(|x| i.cmp(x)){Ok(y) => indices[j][y],Err(y) => indices[j][y - 1],};if masks[k]& new_mask == new_mask { mask = new_mask; i = k; ret.push(b'a' + j asu8);break;}}}String::from_utf8(ret).unwrap()}}